/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docview;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import tableprocessor.PriceDAO;

/**
 *
 * @author Dema
 */
public class CathegoryView extends JPanel implements TreeModelListener,ActionListener,CathegoryListener,TreeSelectionListener {
    
    private JTree CathegoryTree;
    private JButton bAdd;
    private PriceDAO priceDAO;
    private CathegoryListener listener;
    private JScrollPane scrollPane;
    private List<CathegoryEventListener> regListeners = new ArrayList<CathegoryEventListener>();
    public CathegoryView()
    {
        initComponent();
    }

    private void initComponent()
    {
        CathegoryTree = new JTree();
        bAdd = new JButton("Add");
        bAdd.addActionListener(this);
        priceDAO = new PriceDAO();
        BorderLayout layout = new BorderLayout();
        setLayout(layout);
        //add(CathegoryTree,BorderLayout.CENTER);
        //add(bAdd,BorderLayout.SOUTH);
        CathegoryTree.setModel(priceDAO.getTree().getModel());
        CathegoryTree.getModel().addTreeModelListener(this);
        CathegoryTree.addTreeSelectionListener(this);
        scrollPane = new JScrollPane();
        scrollPane.setViewportView(CathegoryTree);
        add (scrollPane,BorderLayout.CENTER);
    }

    public void treeNodesChanged(TreeModelEvent tme)
    {

    }

    public void treeNodesInserted(TreeModelEvent tme) {

    }

    public void treeNodesRemoved(TreeModelEvent tme) {

    }

    public void treeStructureChanged(TreeModelEvent tme) {

    }

    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if (source == bAdd)
        {
           DefaultMutableTreeNode node = (DefaultMutableTreeNode)CathegoryTree.getSelectionPath().getLastPathComponent();
           addEvent(node.toString());

        }
    }

    public void setListener(CathegoryListener _listener)
    {
        listener = _listener;
    }
    public void registerEventListener(CathegoryEventListener _listener)
    {
        regListeners.add(_listener);
    }
    public void addEvent(String name) {
        listener.addEvent(name);
    }

    public void valueChanged(TreeSelectionEvent e)
    {
        if (e.getNewLeadSelectionPath()!=null)
        {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getNewLeadSelectionPath().getLastPathComponent();
          for (int i = 0 ; i < regListeners.size(); i++)
          {
              regListeners.get(i).selectCathegoryEvent(node);
          }
        }
    }

}
